
import 'dart:io';

import 'package:baselib/utils/gallery_camera_pick_util.dart';
import 'package:baselib/utils/load_util.dart';
import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/navigator_util.dart';
import 'package:baselib/utils/time_util.dart';
import 'package:baselib/utils/toast_util.dart';
import 'package:flutter/material.dart';
import 'package:framelib/base/base_view_model.dart';
import 'package:framelib/config/api_config.dart';
import 'package:framelib/config/route_config.dart';
import 'package:framelib/net/baseentity.dart';
import 'package:framelib/utils/color_util.dart';
import 'package:framelib/utils/oss_util.dart';
import 'package:framelib/utils/select_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:framelib/widgets/select_gender_widget.dart';
import 'package:module_business/module_comm/model/user_entity.dart';
import 'package:module_business/module_comm/model/user_params_model.dart';
import 'package:module_business/module_my/service/module_my_service.dart';
import 'package:get/get.dart';

import 'my_view_model.dart';

class ProfileEditViewModel extends BaseViewModel<ModuleMyService>{

  ModuleMyService moduleMyService;
  UserEntity myInfo = UserEntity();

  File iconFile;

  @override
  ModuleMyService createService() =>moduleMyService = ModuleMyService();
  @override
  void onInit() {
    LogUtil.d('MyViewModel-->onInit');
    super.onInit();
    myInfo = UserParamsModel.instance.userEntity??UserEntity();
  }

  onIconPressed() async{
    iconFile = await GalleryCameraPickUtil.instance.selectPicture();
    if(iconFile==null)return;
    LoadUtil.showLoad(loadingText: '${StringUtil.instance.Uploading}...');
    OssUtil.instance.upload(iconFile, complete: (path,isUpload){
      LoadUtil.hideLoad();
      myInfo?.icon = path;
      update();
    });
  }

  itemTap(String tag) {
    if(tag==StringUtil.instance.Signature){
      NavigatorUtil.pushByName(RouteConfig.editSignaturePage,
          arguments: {'signature':myInfo?.signature??''},
          resultFunc: (value){
        if((value!=null)&&(value is Map)&&(value['result']=='ok')){
          myInfo.signature = value['signature'];
          update();
        }
      });
    }
    if(tag==StringUtil.instance.Name){
      NavigatorUtil.pushByName(RouteConfig.editNamePage,
          arguments: {'name':myInfo?.nickname??''},
          resultFunc: (value){
        if((value!=null)&&(value is Map)&&(value['result']=='ok')){
          myInfo.nickname = value['name'];
          update();
        }
      });
    }
  }

  selectGender() {
    showModalBottomSheet(
        context: Get.context,
        isScrollControlled: true,
        backgroundColor: ColorUtil.instance.color_transparent,
        builder: (BuildContext context){
          return SelectGenderWidget(
            maleSelectedFunc: (){
              LogUtil.d('selectGender--->maleSelectedFunc');
              myInfo?.gender = 1;
              update();
            },
            femaleSelectedFunc: (){
              LogUtil.d('selectGender--->femaleSelectedFunc');
              myInfo?.gender = 2;
              update();
            },
          );
        }
    );
  }

  String getGender() {
    int genderCurIndex = myInfo?.gender??0;
    if(genderCurIndex == 1)return StringUtil.instance.Male;
    if(genderCurIndex == 2)return StringUtil.instance.Female;
    return '';
  }

  onBirthPressed() {
    SelectUtil.showDatetimePicker(
        Get.context,
        initialDateTime: TimeUtils.getTimeDate(myInfo?.birthday),
        onConfirm: (DateTime birthday){
          print('birthday--->$birthday');
          myInfo?.birthday = TimeUtils.forMatTimeByDateTime(birthday,format: 'yyyy-MM-dd');
          update();
        }
    );
  }

  submit() async{
    LoadUtil.showLoad(loadingText: StringUtil.instance.Updating);
    BaseEntity baseEntity = await moduleMyService.api_user_updateUserInfo(parmsMap: myInfo?.toJson());
    LoadUtil.hideLoad();
    if(baseEntity?.code == ApiConfig.SUCCESS_CODE){
      NavigatorUtil.goBack();
      MyViewModel myViewModel = Get.find();
      if(myViewModel!=null)myViewModel.onRefresh();
    }else{
      ToastUtil.toast(baseEntity?.message);
    }
  }
}
